/**
 * Copyright (C) 2018-2020
 * All rights reserved, Designed By www.yixiang.co
 * 注意：
 * 本软件为www.yixiang.co开发研制，未经购买不得使用
 * 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 * 一经发现盗用、分享等行为，将追究法律责任，后果自负
 */
package co.yixiang.modules.auction.service.mapper;

import co.yixiang.common.mapper.CoreMapper;
import co.yixiang.modules.auction.domain.AuctionProductD;
import co.yixiang.modules.auction.domain.YxAuctionRecord;
import co.yixiang.modules.auction.service.dto.YxAuctionRecordDto;
import co.yixiang.modules.user.domain.YxUserBill;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author hupeng
 * @date 2020-09-15
 */
@Repository("YxAuctionRecordMapper")
public interface YxAuctionRecordMapper extends CoreMapper<YxAuctionRecord> {

    @Select("select * from yx_auction_record where time_id=#{timeId} and year(start_time)=year(now()) and month(start_time)=month(now()) and day(start_time)=day(now()) order by is_buy asc limit 3")
    List<YxAuctionRecordDto> selectByExampleProByTimeId(@Param("timeId") long time);


    @Select("select * from yx_auction_record where id=#{id}")
    YxAuctionRecordDto getYxAuctionProductDetail(Long id);

    @Select(" select yac.* , yacf.start_time as sTime , yacf.end_time as eTime , yar2.price as buyPrice from yx_auction_record yac LEFT JOIN " +
            " yx_auction_config yacf on yac.time_id = yacf.id " +
            " LEFT JOIN yx_auction_record yar2 on yac.up_id = yar2.id where yac.user_id= #{uid} and yac.is_buy=0 and " +
            " ((TO_DAYS(yac.start_time)>TO_DAYS(now())) or (TO_DAYS(yac.start_time)=TO_DAYS(now()) and yac.time_id in (#{timeId}))) " +
            " order by yac.id desc")
    Page<YxAuctionRecordDto> waitOrderPage(@Param("page") Page page1,@Param("uid") Long uid,@Param("timeId") String timeId);

    @Select(" select yac.* , yacf.start_time as sTime , yacf.end_time as eTime , yar2.price as buyPrice" +
            " from yx_auction_record yac LEFT JOIN " +
            " yx_auction_config yacf on yac.time_id = yacf.id " +
            " LEFT JOIN yx_auction_record yar2 on yac.up_id = yar2.id where yac.user_id= #{uid} and yac.is_buy=0 and " +
            " (TO_DAYS(yac.start_time)=TO_DAYS(now())) " +
            " order by yac.id desc ")
    Page<YxAuctionRecordDto> inOrderPage(@Param("page") Page page1,@Param("uid") Long uid);

    @Select("select yac.* , yacf.start_time as sTime , yacf.end_time as eTime , yar2.price as buyPrice " +
            " , yao.pay_time as payTime from yx_auction_record yac LEFT JOIN " +
            " yx_auction_config yacf on yac.time_id = yacf.id " +
            " LEFT JOIN yx_auction_record yar2 on yac.up_id = yar2.id " +
            " LEFT JOIN yx_auction_order yao on yao.auction_record_id = yac.id " +
            " where yac.user_id= #{uid} and yac.is_buy=1 and yac.status !=3 and yao.`status` != 5 " +
            " order by yac.id desc")
    Page<YxAuctionRecordDto> confirmOrderPage(Page page1, Long uid);

    @Select("select yac.* , yacf.start_time as sTime , yacf.end_time as eTime ," +
            "  yar2.price as buyPrice , yao.pay_time as payTime from yx_auction_record yac LEFT JOIN " +
            " yx_auction_config yacf on yac.time_id = yacf.id " +
            " LEFT JOIN yx_auction_record yar2 on yac.up_id = yar2.id " +
            " LEFT JOIN yx_auction_order yao on yao.auction_record_id = yac.id " +
            " where yac.user_id= #{uid} and yac.is_buy=1 and yac.status =3 " +
            " order by yac.id desc")
    Page<YxAuctionRecordDto> overOrderPage(Page page1, Long uid);

    @Select(" select yac.* , yacf.start_time as sTime , yacf.end_time as eTime , yar2.price as buyPrice from yx_auction_record yac LEFT JOIN " +
            " yx_auction_config yacf on yac.time_id = yacf.id " +
            " LEFT JOIN yx_auction_record yar2 on yac.up_id = yar2.id where yac.user_id= #{uid} and yac.is_buy=0 and " +
            " (TO_DAYS(yac.start_time)>TO_DAYS(now())) " +
            " order by yac.id desc")
    Page<YxAuctionRecordDto> waitOrderPageByTime(Page page1, Long uid);

    @Select("select * from yx_auction_record where id=#{id} and user_id=#{uid}")
    YxAuctionRecordDto productReocrdDetail(@Param("id") Long id, @Param("uid")Long uid);

    @Select("select count(1) from yx_auction_record where  user_id=#{uid} and (is_buy =0 or status!=3) ")
    Integer selectCountByUid(Long uid);

    @Select("select * from yx_auction_record where  is_buy =0 and TO_DAYS(start_time) = TO_DAYS(now()) ")
    List<YxAuctionRecord> selectNoBuy();

    @Select("<script> select ar.price,ar.is_buy,ar.is_max,ar.id,ar.auction_product_id from yx_auction_record ar left join yx_auction_product ap on ar.auction_product_id=ap.id where year(ar.start_time)=year(now())  <if test=\"searchName !=null and searchName !=''\"> and ap.title like concat('%',#{searchName},'%')</if>  and month(ar.start_time)=month(now()) and day(ar.start_time)=day(now()) order by ar.is_buy asc,ar.create_time desc </script>")
    Page<YxAuctionRecordDto> getSearchAuctionProductPageList(@Param("page") Page page1, @Param("searchName") String searchName);

    @Select("<script> select ar.price,ar.is_buy,ar.is_max,ar.id,ar.auction_product_id from yx_auction_record ar left join yx_auction_product ap on ar.auction_product_id=ap.id where year(ar.start_time)=year(now())  <if test=\"isBuy !=null and isBuy !=''\"> and ar.is_buy =#{isBuy}</if>  and month(ar.start_time)=month(now()) and day(ar.start_time)=day(now()) order by ar.is_buy asc,ar.create_time desc </script>")
    Page<YxAuctionRecordDto> getSearchAuctionProductPageLists(@Param("page") Page page1, @Param("isBuy") Integer isBuy);


    @Select("<script> select ar.price,ar.is_buy,ar.is_max,ar.id,ar.auction_product_id from yx_auction_record ar left join yx_auction_product ap on ar.auction_product_id=ap.id where year(ar.start_time)=year(now())  <if test=\"searchName !=null and searchName !=''\"> and ap.title like concat('%',#{searchName},'%')</if>  and month(ar.start_time)=month(now()) and ar.time_id=#{time} and day(ar.start_time)=day(now()) order by ar.is_buy,ar.id asc </script>")
    Page<YxAuctionRecordDto> getYxAuctionProductPageList(@Param("page") Page page,@Param("searchName") String searchName,@Param("time") long time);

    @Select("<script> select ar.price,ar.is_buy,ar.is_max,ar.id,ar.auction_product_id from yx_auction_record ar left join yx_auction_product ap on ar.auction_product_id=ap.id where year(ar.start_time)=year(now())  <if test=\"isBuy !=null and isBuy !=''\"> and ar.is_buy =#{isBuy}</if>  and month(ar.start_time)=month(now()) and ar.time_id=#{time} and day(ar.start_time)=day(now()) order by ar.is_buy,ar.id asc </script>")
    Page<YxAuctionRecordDto> getYxAuctionProductPageLists(@Param("page") Page page,@Param("isBuy") Integer isBuy,@Param("time") long time);


    @Select("<script> select count(1) from yx_auction_record ar " +
            "left join yx_auction_product ap on ar.auction_product_id=ap.id" +
            " where TO_DAYS(ar.start_time)= TO_DAYS(now()) and ar.time_id=#{time} </script>")
    Integer getYxAuctionProductCount(@Param("time") long time);

    @Select("<script> select ar.price,ar.is_buy,ar.is_max,ar.id,ar.auction_product_id from yx_auction_record ar " +
            "left join yx_auction_product ap on ar.auction_product_id=ap.id" +
            " where TO_DAYS(ar.start_time)= TO_DAYS(now()) and ar.time_id=#{time} order by ar.is_buy,ar.id asc </script>")
    Page<YxAuctionRecordDto> getYxAuctionProductList(@Param("page") Page page,@Param("time") long time);

    @Select(" select id,title,image from yx_auction_product  ")
    List<AuctionProductD> getListProduct();

    @Select(" SELECT IFNULL(balance,0) from yx_user_bill where category = 'now_money' and create_time < \"2021-04-10 10:00:00\" " +
            " and uid = #{uid} ORDER BY create_time desc limit 1  ")
    BigDecimal getBB1Money(Long uid);

    @Select(" SELECT IFNULL(SUM(number),0) from yx_user_bill where category = 'now_money' and create_time >= \"2021-04-10 10:00:00\" and uid = #{uid} and pm = 1  ")
    BigDecimal getBB2Money(Long uid);

    @Select(" SELECT IFNULL(SUM(number),0) from yx_user_bill where category = 'now_money' and create_time >= \"2021-04-10 10:00:00\" and uid = #{uid} and pm = 0  ")
    BigDecimal getBB3Money(Long uid);

    @Select(" SELECT * from yx_user_bill where category = 'now_money' and create_time >= \"2021-04-10 10:00:00\" and uid = #{uid} ")
    List<YxUserBill> getUserBillList(Long uid);
}
